//
//  JavaDate.m
//
//  Created by GaoShin on 2020/6/28.
//

#import "JavaDate.h"

@implementation JavaDate
{
    NSDate * internalDate;
}

-(instancetype) init{
    self = [super init];
    if(self){
        self->internalDate = [NSDate date];
    }
    return self;
}

- (instancetype) initWithTimeInMillis:(long long)timeInMillis{
    self = [super init];
    if(self){
        NSTimeInterval tempMilli = timeInMillis;
        NSTimeInterval seconds = tempMilli/1000.0;//这里的.0一定要加上，不然除下来的数据会被截断导致时间不一致
        self->internalDate = [NSDate dateWithTimeIntervalSince1970:seconds];
    }
    return self;
}

- (instancetype)initWithTimeInSeconds:(long)timeInSeconds{
    self = [super init];
    if(self){
        NSTimeInterval tempMilli = timeInSeconds;
        NSTimeInterval seconds = tempMilli;
        self->internalDate = [NSDate dateWithTimeIntervalSince1970:seconds];
    }
    return self;
}

-(instancetype) initWithNSDate:(NSDate *)date{
    self = [super init];
    if(self){
        self->internalDate = [date copy];
    }
    return self;
}

+(instancetype) javaDateFromNSDate:(NSDate *) date{
    return [[JavaDate alloc] initWithNSDate:date];
}

-(long long) getTimeInMillis {
    NSTimeInterval interval = [internalDate timeIntervalSince1970];
    long long totalMilliseconds = interval*1000 ;
    return totalMilliseconds;
}

-(NSDate *) getDate {
    return internalDate;
}
@end
